//
//  HMMainTabBar.swift
//  01-SinaWeibo
//
//  Created by 周英培 on 16/5/20.
//  Copyright © 2016年 周英培. All rights reserved.
//

import UIKit

class HMMainTabBar: UITabBar {
    
    //在init方法中添加子视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加➕按钮
        addSubview(plusBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //布局自定义的tabbar里面的子控件的frame
    override func layoutSubviews() {
        
          super.layoutSubviews()
        
        //设置每个控件的宽高和frame
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //遍历子视图，设置frame
        //设置控件的索引Index
        var index :CGFloat = 0
        
        for subView in subviews {
            
            //判断子控件里面哪些是UITabBarButton的类型的
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                //设置frame
//                subView.frame = CGRect(x: w * index, y: 0, width: w, height: h)
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                //判断让中间那个➕控件添加到哪个位置，就在那个位置流出位置
                if index == 1 {
                    index += 1
                }
                index += 1
             
            }
            
        }
        
        plusBtn.frame = CGRectOffset(rect, w * 2, 0)
        
//        //让➕按钮的位置偏上
//        plusBtn.frame = CGRectOffset(rect, w * 2, -20)
//        
//        //让某个按钮处于最上面
//        bringSubviewToFront(plusBtn)
        
//        super.layoutSubviews()
    }
    //MARK:➕按钮 使用懒加载获取
    lazy var plusBtn :UIButton = {
        
        //alloc/init创建获得的也是自定义的按钮
        let btn = UIButton()
        
        //设置图片
        btn .setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        return btn
    }()
    
    
}
